import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/Login.vue'
import Token from '@/api/token.js'


const routes = [
  {
    path: '/login',
    name: 'LoginView',
    component: LoginView
  },
  {
    path: '/',
    name: 'HomeView',
    component: HomeView,
  },
  {
    path: '/class-manage',
    name: 'ClassView',
    component: () => import('../views/ClassView.vue'),
  },
  {
    path: '/lang-manage',
    name: 'LangView',
    component: () => import('../views/LangView.vue'),
  },
  {
    path: '/module-manage',
    name: 'ModuleView',
    component: () => import('../views/ModuleView.vue'),
  },
  {
    path: '/student-manage',
    name: 'StudentView',
    component: () => import('../views/StudentView.vue'),
  },
  {
    path: '/enterprise-manage',
    name: 'EnterpriseView',
    component: () => import('../views/EnterpriseView.vue'),
  },
  {
    path: '/role-manage',
    name: 'RoleView',
    component: () => import('../views/RoleView.vue'),
  },
  {
    path: '/task-manage',
    name: 'TaskView',
    component: () => import('../views/TaskView.vue'),
  },
  {
    path: '/menu-manage',
    name: 'MenuView',
    component: () => import('../views/MenuView.vue')
  },
  {
    path: '/func-manage', 
    name: 'FuncView',
    component: () => import('../views/FuncView.vue')
  },
  {
    path: '/permission-manage',
    name: 'PermissionView', 
    component: () => import('../views/PermissionView.vue')
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})

// 设置全局路由守卫，拦截除了登录之外的所有路由
router.beforeEach((to, from, next)=>{
  // 1. 判断是否登录
  if (to.name === 'LoginView'){
    next()
    return
  }
  // 判断当前用户是否登录
  if(Token.existsRefreshToken()) {
    // 登录了，放行,跳转到目标路由
    next()
    return
  }
  // 2. 没有登录，拦截，跳转到登录页面
  next({name: 'LoginView'})
  return
})
export default router
